package com.lang.service.impl;

import com.lang.dao.AddressDao;
import com.lang.dao.CartDao;
import com.lang.dao.OrderDao;
import com.lang.dao.impl.AddressDaoImpl;
import com.lang.dao.impl.CartDaoImpl;
import com.lang.dao.impl.OrderDaoImpl;
import com.lang.entity.Address;
import com.lang.entity.Cart;
import com.lang.entity.Item;
import com.lang.entity.Orders;
import com.lang.service.OrderService;
import com.lang.utils.RandomUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
 * @Date : 2024/3/14 10:51
 * @Created by Lang
 * @ProjectName : LclShop
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private CartDao cartDao = new CartDaoImpl();
    @Override
    public int addOrder(String aid, String sum, int uId) {
        //添加订单
        Orders orders = new Orders();
        orders.setO_id(RandomUtils.createOrderId());
        orders.setA_id(Integer.parseInt(aid));
        orders.setU_id(uId);
        orders.setO_count(new BigDecimal(sum));
        orders.setO_time(new Date());
        orders.setO_state(1);
        int res = orderDao.addOrder(orders);
        System.out.println("订单添加=" + res);

        //订单详情
        //创建一个集合用于存放item对象   一个订单会有多个订单详情，也就是一种商品
        List<Item> items = new ArrayList<>();
        //获取到用户的购物车
        List<Cart> carts = cartDao.getCarts(uId);
        //将购物车里面的商品每一件都放进items里面
        for (Cart cart : carts) {
            Item item = new Item(0,orders.getO_id(),cart.getP_id(), cart.getC_count(),cart.getC_num(),null);
            items.add(item);
        }
        //dao层对items进行添加
        res = orderDao.addItem(items);
        System.out.println("订单详情=" + res);

        return 0;
    }

    @Override
    public List<Orders> showOrder(int uid) {
        return orderDao.showOrder(uid);
    }

    @Override
    public Orders showItems(String oid) {
        //order-address
        //item-product
        Orders orders = orderDao.showOrderByOid(oid);
        List<Item> items = orderDao.getItems(oid);
        orders.setItems(items);

        return orders;
    }
}
